﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Printing;
using System.Windows.Forms;

using GReporting.Core.Layout;
namespace GReporting.Core.Export
{
    /// <summary>
    /// 用于打印报表
    /// </summary>
    public class GPrint
    {
        // 打印对象
        private System.Drawing.Printing.PrintDocument _PrintDocument;
        //打印预览窗口
        private System.Windows.Forms.PrintPreviewDialog _printPreviewDialog;
        //打印报表
        private GReports _report;
        //打印设置
        private GPrintSetting _setting;
        private int _pageNo=0;
        public GPrint(GReports report,GPrintSetting setting)
        {
            this._report = report;
            this._setting = setting;
            this._PrintDocument = new System.Drawing.Printing.PrintDocument();
            this._printPreviewDialog = new System.Windows.Forms.PrintPreviewDialog();
            this._PrintDocument.PrintPage += new PrintPageEventHandler(PrintPage);
            this._PrintDocument.BeginPrint += new PrintEventHandler(BeginPrint);
            this._PrintDocument.EndPrint += new PrintEventHandler(EndPrint);
            Init();
        }
        //加载setting
        private void Init()
        {
            _PrintDocument.DefaultPageSettings.PaperSize = _setting.PaperSize;
            _PrintDocument.DefaultPageSettings.Landscape = false;

        }
        private void BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {

        }

        private void PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            if (_pageNo < _report.Pages.Count)
            {
                GPage page = _report.Pages[_pageNo];
                //遍历打印每页的元素
                foreach (GElement element in page.Elements)
                {
                    GPrintElement printElement = PrintElementFactory.GetElement(element);
                    printElement.Print(e.Graphics);
                }
                _pageNo++;
            }
            else//空白
            {
                e.HasMorePages = false;
            }
            if (_pageNo > _report.Pages.Count - 1)
            {
                e.HasMorePages = false;
            }
            else
            {
                e.HasMorePages = true;
            }
            
        }

        private void EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {

        }
        /// <summary>
        ///  打印报表
        /// </summary>
        /// <param name="isPreview">是否带预览窗</param>
        public void Print(bool isPreview)
        {

            this._printPreviewDialog.Document = this._PrintDocument;
            //创建虚表
            try
            {
                if (isPreview)
                {
                    if (this._printPreviewDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {

                        _PrintDocument.Print();
                    }
                }
                else
                {
                    _PrintDocument.Print();

                }
            }
            catch (System.Drawing.Printing.InvalidPrinterException e)
            {
                MessageBox.Show("未安装打印机，请进入系统控制面版添加打印机！", "打印", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "打印", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}
